package com.sirui.jpa.utils;

import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import lombok.extern.apachecommons.CommonsLog;

import java.text.SimpleDateFormat;
import java.util.Date;

@CommonsLog
public class P6SpyLogger implements MessageFormattingStrategy {
    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        if (!"".equals(sql.trim())) {
            StringBuffer buffer = new StringBuffer();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            if (!("").equals(sql.trim())) {
                return buffer.append("/* ").append(simpleDateFormat.format(new Date())).append(" | took ")
                        .append(elapsed).append("ms | ").append(category)
                        .append(" | connection ").append(connectionId).append(" */")
                        .append(System.lineSeparator())
                        .append("执行语句：")
                        .append(sql).append(";").toString();
            }
        }
        return "";
    }
}
